package de.rts.components;

import java.awt.Color;
import java.awt.Point;
import java.util.LinkedList;
import java.util.List;

public class Segment implements RailComponent
{

  private Point        position;
  private Point        size;
  private Point        labelPosition;
  private Point[]      switchCoordinates;
  private String       segmentId;
  private boolean      occupied;
  private boolean      switchSegment;
  private boolean      switchEnabled;
  private List<Sensor> sensorList = new LinkedList<Sensor>();

  public Segment(String id)
  {
    this(new Point(), new Point(), id);
  }

  public Segment(Point position, Point size, String id)
  {
    super();
    try
    {
      if (position == null || size == null) throw new RailwayComponentException("Initialization error");
    }
    catch (RailwayComponentException e)
    {
      e.printStackTrace();
    }
    this.size = size;
    this.position = position;
    this.segmentId = id;
  }

  public Point getPosition()
  {
    return position;
  }

  public void setPosition(Point position)
  {
    this.position = position;
  }

  public Point getSize()
  {
    return size;
  }

  public void setSize(Point size)
  {
    this.size = size;
  }

  public int getXPosition()
  {
    return position.x;
  }

  public int getYPosition()
  {
    return position.y;
  }

  public int getWidth()
  {
    return size.x;
  }

  public int getHeight()
  {
    return size.y;
  }

  public String getSegmentId()
  {
    return segmentId;
  }

  public boolean isOccupied()
  {
    return occupied;
  }

  public void setOccupied(boolean occupied)
  {
    this.occupied = occupied;
  }

  public boolean isSwitchSegment()
  {
    return switchSegment;
  }

  public void setSwitchSegment(boolean switchSegment)
  {
    this.switchSegment = switchSegment;
  }

  public boolean isSwitchEnabled()
  {
    return switchEnabled;
  }

  public void setSwitchEnabled(boolean switchEnabled)
  {
    this.switchEnabled = switchEnabled;
  }

  public List<Sensor> getSensorList()
  {
    return sensorList;
  }

  public Sensor getSensor(int index)
  {
    return sensorList.get(index);
  }

  public Color getSegmentColor()
  {
    if (occupied)
      return Color.RED;
    return Color.GREEN;
  }

  public Point[] getSwitchCoordinates()
  {
    return switchCoordinates;
  }

  public void setSwitchCoordinates(Point[] switchCoordinates)
  {
    if (!switchSegment)
    {
      System.err.println("segment has no switch.");
      return;
    }
    this.switchCoordinates = switchCoordinates;
  }

  public Point getLabelPosition()
  {
    return labelPosition;
  }

  public void setLabelPosition(Point labelPosition)
  {
    this.labelPosition = labelPosition;
  }

  public int[] getSwitchXCoordinates()
  {
    int[] xcoord = new int[switchCoordinates.length];
    for (int i = 0; i < switchCoordinates.length; i++)
      xcoord[i] = switchCoordinates[i].x;
    return xcoord;
  }

  public int[] getSwitchYCoordinates()
  {
    int[] ycoord = new int[switchCoordinates.length];
    for (int i = 0; i < switchCoordinates.length; i++)
      ycoord[i] = switchCoordinates[i].y;
    return ycoord;
  }

  @Override
  public String toString()
  {
    return "Segment@"+ hashCode() +" [position=" + position + ", segmentId=" + segmentId + ", occupied=" + occupied + ", switchSegment="
      + switchSegment + ", switchEnabled=" + switchEnabled + "]";
  }
}
